import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import legacy from '@vitejs/plugin-legacy'
import pages from 'vite-plugin-pages'
import px2vp from 'postcss-px2vp'
import { createStyleImportPlugin, VantResolve } from 'vite-plugin-style-import'

// 设计稿的视口宽度
const viewportWidth = 375

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        pages({
            exclude: ['**/components/*.vue'],
            importMode: 'async',
        }),
        // TODO: 升级vite-plugin-style-import,
        // Cannot find module 'consola'， 临时手动加上此依赖，待升级后删除
        createStyleImportPlugin({
            resolves: [VantResolve()],
        }),
        // legacy({
        //   targets: ['defaults', 'not IE 11'],
        // }),
    ],
    css: {
        postcss: {
            plugins: [
                px2vp({
                    viewportWidth(rule) {
                        const file = rule.source ?.input.file
                        if (file ?.includes('vant')) return 375
                        return viewportWidth
                    },
                }),
            ],
        },
    },
    server: {
        open: true,
        proxy: {
            '/api': {
                target: 'https://wealth-public-dev.pinnacle-mobileapp.dev.ali.cloud.cn.hsbc',
                changeOrigin: true,
                secure: false,
            },
        },
    },
    build: {
        rollupOptions: {
            output: {
                manualChunks: {
                    base: ['vue', 'vue-router'],
                },
            },
        },
    },
})